package com.ssps.back.platform.service.Impl;

import com.ssps.back.platform.service.OwnerService;
import com.ssps.entity.*;
import com.ssps.mapper.OwnerMapper;
import com.ssps.mapper.ParkingMapper;
import com.ssps.mapper.UserInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author li
 * @time 2020/07/24 14:05:06
 */

@Service
public class OwnerServiceImpl implements OwnerService {

    @Autowired
    OwnerMapper ownerMapper;
    @Autowired
    UserInfoMapper userInfoMapper;
    @Autowired
    ParkingMapper parkingMapper;


    @Override
    public List<Owner> ownerFindAll(String status, String uname) {
        OwnerExample example=new OwnerExample();
        OwnerExample.Criteria criteria = example.createCriteria();
        if (status!=""&&status!=null){
            criteria.andStatusEqualTo(status);
        }
        List<Integer>uuid=new ArrayList<>();
        if (uname!=""&&uname!=null){
            UserInfoExample example1=new UserInfoExample();
            UserInfoExample.Criteria criteria1 = example1.createCriteria();
            criteria1.andUnameLike("%"+uname+"%");
           List<UserInfo> userInfoList=userInfoMapper.selectByExample(example1);

            if (userInfoList!=null) {
                for (UserInfo userInfo : userInfoList) {
                    uuid.add(userInfo.getUiid());
                }
                criteria.andUiidIn(uuid);
            }
        }
        List<Owner>ownerList=ownerMapper.selectByExample(example);
        if (ownerList!=null){
            for (Owner owner : ownerList) {
                UserInfo userInfo=userInfoMapper.selectByPrimaryKey(owner.getUiid());
                owner.setUserInfo(userInfo);
            }
        }
        return ownerList;
    }

    @Override
    public void ownerUpdate(Owner owner) {
        Owner newOwner=ownerMapper.selectByPrimaryKey(owner.getOid());
        ParkingExample example=new ParkingExample();
        ParkingExample.Criteria criteria = example.createCriteria();
        criteria.andUiidEqualTo(newOwner.getUiid());
        List<Parking> parkingList=parkingMapper.selectByExample(example);
        if (parkingList!=null){
            for (Parking parking : parkingList) {
                parking.setStatus("未出租");
                parkingMapper.updateByPrimaryKeySelective(parking);
            }
        }
        ownerMapper.updateByPrimaryKeySelective(owner);
    }
}
